/**
 * Created by Administrator on 2017/4/6.
 */

$(function () {
    //打开课程安排
    $("#layerplan11111").click(function(){
        layer.open({
            type: 1,
            anim: 2,
            title:false,
            area:500,
            time:60000, // 自动关闭
            closeBtn: 1, //不显示关闭按钮
            shadeClose: true, //开启遮罩关闭
            content: $('.layerplan'),
        });
        var cur = $(".nav_tab_1").html();
        if(!$.trim(cur)){
            $.ajax({
                type: 'get',
                url: '/roomcourse',
                success: function(data){
                    var week = '';
                    var action = '';
                    for(i=0;i < data.length;i++){
                        if(i == 1){action = 'cur';}else{action = '';}
                        week +='<li class="'+action+'" data-time="'+data[i].course_time+'">'+getMyDay(new Date(data[i].course_time))+'('+data[i].course_time.substring(5,10)+')</li>';
                    }
                    $(".nav_tab_1").append(week);
                    $('.cur').click();

                }
            })
        }

    });

    $(document).on('click',".nav_tab_1 li",(function(){
        $(".nav_tab_1 li").eq($(this).index()).addClass("cur").siblings().removeClass('cur');
        $(".con_cell_1").hide().eq($(this).index()).show();
        var cur = $(".con_cell_1").eq($(this).index()).html();
        var eq = $(this).index()
        if(!$.trim(cur)){
            var time = $(this).data('time');
            WeekCourse(time,eq)
        }
    }));
    //获取课程内容
    function WeekCourse(time,eq){
        $.ajax({
            type: 'get',
            url: '/weekcourse',
            data:{time:time},
            success: function(data){
                var course ='<ul>';
                for(i=0;i < data.length;i++){
                    course +='<li class="'+ThinTime(data[i].course_time,data[i].first_time.substring(0,5),data[i].end_time.substring(0,5))+'"><span><p>'+data[i].first_time.substring(0,5)+'-'+data[i].end_time.substring(0,5)+'</p><strong>'+data[i].name+'</strong><h2>'+data[i].course_name+'</h2></span></li>';
                }
                course +='</ul>';
                $(".con_cell_1").eq(eq).append(course);
            }
        })
    }
    //获取当前星期几
    function getMyDay(date){
        var week;
        if(date.getDay()==0) week="周日"
        if(date.getDay()==1) week="周一"
        if(date.getDay()==2) week="周二"
        if(date.getDay()==3) week="周三"
        if(date.getDay()==4) week="周四"
        if(date.getDay()==5) week="周五"
        if(date.getDay()==6) week="周六"
        return week;
    }
    //判断时候在当前时间内
    function ThinTime(course,start,end){
        start = course+' '+start;
        end = course+' '+end;
        var starttime = $.myTime.DateToUnix(start);
        var endtime = $.myTime.DateToUnix(end);
        //当前时间戳
        var datetime = $.myTime.CurTime();
        var thinktime = '';
        if(starttime <= datetime && endtime >= datetime){
            thinktime = 'con_cellActive_1';
        }
        return thinktime;
    }

    /**
     * 日期 转换为 Unix时间戳
     * @param <string> 2014-01-01 20:20:20  日期格式
     * @return <int>        unix时间戳(秒)
     */
    (function($) {
        $.extend({
            myTime: {
                /**
                 * 当前时间戳
                 * @return <int>        unix时间戳(秒)
                 */
                CurTime: function(){
                    return Date.parse(new Date())/1000;
                },
                DateToUnix: function(string) {
                    var f = string.split(' ', 2);
                    var d = (f[0] ? f[0] : '').split('-', 3);
                    var t = (f[1] ? f[1] : '').split(':', 3);
                    return (new Date(
                            parseInt(d[0], 10) || null,
                            (parseInt(d[1], 10) || 1) - 1,
                            parseInt(d[2], 10) || null,
                            parseInt(t[0], 10) || null,
                            parseInt(t[1], 10) || null,
                            parseInt(t[2], 10) || null
                        )).getTime() / 1000;
                }
            }
        });
    })(jQuery);
});